package com.happyhome.dao;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

import com.happyhome.constants.ComUnitName;
import com.happyhome.constants.RecordStatus;
import com.happyhome.datamodel.News;
import com.happyhome.datamodel.QNews;
import com.mysema.query.BooleanBuilder;
import com.mysema.query.jpa.JPQLQuery;
import com.mysema.query.jpa.impl.JPAQuery;
import com.trg.dao.jpa.GenericDAOImpl;

@Repository
public class NewsDaoImpl extends GenericDAOImpl<News, Long> implements NewsDao{
	
	@SuppressWarnings("unused")
	private static final Logger LOGGER = LoggerFactory.getLogger(NewsDaoImpl.class);
	
	@Override
	@PersistenceContext(unitName = ComUnitName.COM_UNIT)
	public void setEntityManager(EntityManager entityManager) {
		super.setEntityManager(entityManager);
	}

	@Override
	public List<News> getAllNews() {
		JPQLQuery query = new JPAQuery(em());
		QNews news = QNews.news;
		
		query.from(news);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(news.status.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<News> resultNews = query.list(news);
		return resultNews;
	}
	
	@Override
	public List<News> getListNewsInListId(List<Long> listNews){
		JPQLQuery query = new JPAQuery(em());
		QNews news = QNews.news;
		
		query.from(news);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(news.status.eq(RecordStatus.TRUE))
			 .and(news.idNews.in(listNews));
		
		query.where(where);
		
		List<News> resultNews = query.list(news);
		return resultNews;
	}
	
	@Override
	public List<News> getListNewsNotInListId(List<Long> listNews){
		JPQLQuery query = new JPAQuery(em());
		QNews news = QNews.news;
		
		query.from(news);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(news.status.eq(RecordStatus.TRUE))
			 .and(news.idNews.notIn(listNews));
		
		query.where(where);
		
		List<News> resultNews = query.list(news);
		return resultNews;
	}

	@Override
	public List<News> getListNewsIsDisplay() {
		JPQLQuery query = new JPAQuery(em());
		QNews news = QNews.news;
		
		query.from(news);
		
		BooleanBuilder where = new BooleanBuilder();
		
		where.and(news.status.eq(RecordStatus.TRUE))
			 .and(news.isDisplay.eq(RecordStatus.TRUE));
		
		query.where(where);
		
		List<News> resultNews = query.list(news);
		return resultNews;
	}
}
